// created by noellynch
// Jun 3, 2009

package com.sceneworker.monitorpopup;

import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.Callable;

import javax.imageio.ImageIO;
import javax.swing.JDialog;
import javax.swing.tree.TreePath;

import com.jme.image.Texture;
import com.jme.renderer.Camera;
import com.jme.scene.Spatial;
import com.jme.system.DisplaySystem;
import com.jme.util.GameTaskQueueManager;
import com.jme2utils.staticutils.TextureUtils;
import com.myjavautils.GenericUtils;
import com.myjavautils.swing.SimpleAlert;
import com.sceneworker.app.globals.SceneWorkerDialogManager;
import com.sceneworker.app.globals.SceneWorkerGlobals;
import com.sceneworker.app.globals.cacheobjects.ISceneWorkerCacheObject;
import com.sceneworker.app.globals.cacheobjects.RenderToTextureCacheObject;
import com.sceneworker.editors.texturepainter.PaintOnTexturePanel;
import com.sceneworker.monitorpopup.panels.ModifyRTTBackgroundColourPanel;
import com.sceneworker.monitorpopup.panels.ModifyRTTCameraPanel;

public class TextureMenu extends BaseMenu {

	private static final String PAINT = "Paint";
	private static final String SAVE_TEXTURE = "Save Texture Image";
	private static final String COPY_REFERENCE = "Copy Reference To Texture";
	private static final String PASTE_SPATIAL = "Paste Spatial";
	private static final String LOCK_TO_DISPLAY_SYSTEM_CAMERA = "Lock To Display System Camera";
	private static final String EDIT_BACKGROUND_COLOUR = "Edit Background Colour";
	private static final String MODIFY_CAMERA = "Modify Camera";
	
	private		Texture							mcl_texture;
	private		RenderToTextureCacheObject		mcl_render;
	
	public TextureMenu(TreePath cl_selectedPath, SceneWorkerPopUp cl_menu) {
		super(cl_menu);
		
		mcl_texture = (Texture)cl_selectedPath.getLastPathComponent();
		
		if(mcl_texture.getImage() != null) {
			mcl_menu.add(buildAMenuItem(PAINT));
			mcl_menu.add(buildAMenuItem(SAVE_TEXTURE));
		}
		
		mcl_menu.add(buildAMenuItem(COPY_REFERENCE));
		
		mcl_menu.addSeparator();
		
		ISceneWorkerCacheObject		lcl_co = SceneWorkerGlobals.inst().getFromCache("rtt" + mcl_texture.hashCode());
		
		if(lcl_co == null) {
			return;
		}
		
		if(!(lcl_co instanceof RenderToTextureCacheObject)) {
			return;
		}
		
		mcl_render = (RenderToTextureCacheObject)lcl_co;
		
		buildMenu();
	}

	@Override
	public void actionPerformed(ActionEvent cl_e) {
		if(cl_e.getActionCommand().equals(EDIT_BACKGROUND_COLOUR)) {
			JDialog		lcl_dlg = SceneWorkerDialogManager.inst().makeADialog(SceneWorkerDialogManager.BACKGROUND_COLOUR_EDITOR);
			lcl_dlg.add(new ModifyRTTBackgroundColourPanel(mcl_render, lcl_dlg));
			lcl_dlg.setVisible(true);
			return;
		}
		
		if(cl_e.getActionCommand().equals(LOCK_TO_DISPLAY_SYSTEM_CAMERA)) {
			GameTaskQueueManager.getManager().update(new Callable<Void>() {
				public Void call() throws Exception {
					Camera		lcl_displaySysCam = DisplaySystem.getDisplaySystem().getRenderer().getCamera();
					mcl_render.mcl_rtt.setCamera(lcl_displaySysCam);
					return null;
				}
			});
		}
		
		if(cl_e.getActionCommand().equals(MODIFY_CAMERA)) {
			JDialog		lcl_dlg = SceneWorkerDialogManager.inst().makeADialog(SceneWorkerDialogManager.MODIFY_RTT_CAMERA);
			lcl_dlg.add(new ModifyRTTCameraPanel(mcl_render, lcl_dlg));
			lcl_dlg.pack();
			lcl_dlg.setVisible(true);
		}
		
		if(cl_e.getActionCommand().equals(COPY_REFERENCE)) {
			mcl_menu.setObjectBuffer(mcl_texture);
		}
		
		if(cl_e.getActionCommand().equals(PASTE_SPATIAL)) {
			GameTaskQueueManager.getManager().update(new Callable<Void>() {
				public Void call() throws Exception {
					mcl_render.mcl_scenes = (Spatial)mcl_menu.getObjectBuffer();
					return null;
				}
			});
		}
		
		if(cl_e.getActionCommand().equals(PAINT)) {
			JDialog		lcl_dlg = SceneWorkerDialogManager.inst().makeADialog(SceneWorkerDialogManager.PAINT_TEXTURE);
			lcl_dlg.add(new PaintOnTexturePanel(mcl_texture, lcl_dlg));
			lcl_dlg.pack();
			lcl_dlg.setVisible(true);
		}
		
		if(cl_e.getActionCommand().equals(SAVE_TEXTURE)) {
			final	File		lcl_f = GenericUtils.getAFile(null, false, true);
			if(lcl_f == null) { return; }
			
			GameTaskQueueManager.getManager().update(new Callable<Void>() {
				public Void call() throws Exception {
					BufferedImage		lcl_im = TextureUtils.createBufferedImageFromTexture(mcl_texture, null, true);
					if(lcl_im == null) { return null; }
					
					// write out the screenshot image to a file.
					try {
						if(lcl_f.getName().contains(".png")) {
							ImageIO.write(lcl_im, "png", lcl_f);
						} else {
							ImageIO.write(lcl_im, "png", new File(lcl_f.getPath() + ".png"));
						}
						
						SimpleAlert.doAlert("File saved.", null);
	                    
	                } catch (IOException e) {
	                    e.printStackTrace();
	                }
					
					return null;
				}
				
			});
			
		}
	}

	@Override
	protected void buildMenu() {
		mcl_menu.add(buildAMenuItem(MODIFY_CAMERA));
		mcl_menu.add(buildAMenuItem(LOCK_TO_DISPLAY_SYSTEM_CAMERA));
		mcl_menu.add(buildAMenuItem(EDIT_BACKGROUND_COLOUR));
		
		if(mcl_menu.getObjectBuffer() instanceof Spatial) {
			mcl_menu.add(buildAMenuItem(PASTE_SPATIAL));
		}
		
	}

}
